<ul class="buttons">
  <%# if !Person.find(current_user.person_id).watched_variables.any?{|var| var.variable_id == @variable.id} %>
    <!-- <li> -->
<%#= icon('watch', watch_variable_path(@variable), nil, nil, 'Watch variable') -%>
<!-- </li> -->
  <%# else %>
    <!-- <li> -->
<%#= icon('stopwatch', watch_variable_path(@variable), nil, nil, 'Stop watching variable') -%>
<!-- </li> -->

  <%# end %>
  <!-- <li> -->
<%#= icon('edit', edit_variable_path(@variable), "Edit Variable", nil, 'Edit Variable') -%>
<!-- </li> -->
<% if logged_in? && !@variable.is_archived? -%>
  <li><%= icon('add_to_cart', add_to_cart_variable_path(@variable), "Add Variable to Cart", {:method => :post}, 'Add Variable to Cart') -%></li>
<% end -%>
</ul>
<% if @variable.is_archived? %>
<h1 style="background-color: red;">THIS VARIABLE HAS BEEN ARCHIVED</h1>
	<p><b>Archived by:
		<div class="box_standout">
          <%= link_to User.find(@variable.archived_by).person.name, person_path(User.find(@variable.archived_by).person) -%> on <%= h @variable.archived_on.strftime('%d/%m/%Y @ %H:%M:%S') -%>
        </div>
</p>
<br/>
		<p><b>Reason:
					        <% unless @variable.archived_reason.blank? -%>
			<div class="box_standout">
				<%= @variable.archived_reason -%>
				</div>
				<% else %>
				<p class="none_text">
		          No reason given
		        </p>
				<% end %>
		</p>
	<% end -%>
<h1 class="contribution_title">Variable: <%= link_to(h(@variable.name), variable_path(@variable)) -%></h1>
<div class="show_basic">
  <!-- <div class="main_content_left_box"> -->
    <div class="box_about_actor">
      <p>
        <b>Title:</b>
        <span class="title"><%=h @variable.name -%></span>
      </p>
      <br/>

      <p>
        <b>Description:</b>
        <% unless @variable.value.blank? -%>
        <div class="box_standout">
          <%= simple_format(white_list(@variable.value)) -%>
        </div>
      <% else -%>
        <p class="none_text">
          No description
        </p>
      <% end -%>
      </p>
      <p>
        <b>Documentation:</b>
        <% unless @variable.document.blank? -%>
        <div class="box_standout">
          <%= link_to(@variable.document + ", page " + @variable.page, open_pdf_variable_url(@variable) + "#page=" + @variable.page,:title=>"Open the PDF document with information about this variable") -%>
        </div>
      <% else -%>
        <p class="none_text">
          No documentation listed
        </p>
      <% end -%>
      </p>

      <p>
        <b>Category:</b>
        <% unless @variable.category.blank? -%>
        <div class="box_standout">
          <%= link_to(@variable.category, by_category_variables_url(:category=>@variable.category),:title=>"Show all variables with this category") -%>
        </div>
      <% else -%>
        <p class="none_text">
          No category
        </p>
      <% end -%>
      </p>
      <p>
        <b>Variable source:</b>
        <% unless @variable.dertype.blank? -%>
        <div class="box_standout">
          <%= simple_format(white_list(@variable.dertype)) -%>
        </div>
      <% else -%>
        <p class="none_text">
          No source
        </p>
      <% end -%>
      </p>
      <p>
        <b>Derivation method:</b>
        <% unless @variable.dermethod.blank? -%>
        <div class="box_standout">
          <%= simple_format(white_list(@variable.dermethod)) -%>
        </div>
      <% else -%>
        <p class="none_text">
          No method
        </p>
      <% end -%>
      </p>
      <p>
        <b>Allowed values:</b>
        <% unless @variable.info.blank? -%>
        <div class="box_standout">
          <%= simple_format(white_list(@variable.info)) -%>
        </div>
      <% else -%>
        <p class="none_text">
          No allowed values
        </p>
      <% end -%>
      </p>
        <b>Dataset:</b>
      <div class="box_standout">
        <%= link_to(Dataset.find(@variable.dataset_id).name, dataset_url(Dataset.find(@variable.dataset_id))) -%>
      </div>
      </p>
      <p>
        <b>Survey:</b>
      <div class="box_standout">
        <%= link_to(Dataset.find(@variable.dataset_id).survey.survey_type.shortname + " " + Dataset.find(@variable.dataset_id).survey.year, survey_url(Dataset.find(@variable.dataset_id).survey)) -%>
      </div>
      </p>

      <p>
        <b>Statistics:</b>
        <div class="box_standout">
          <%= render :partial => "variables/statistics_table",:locals=>{:variable => @variable} %>
        </div>
      </p>
      <!-- TODO Style cleanup Including highlighting header line. -->
      <% no_var_hash = @variable.none_values_hash -%> 

     <!-- TODO add a paramter to control when it long values lists - could be in the thousands -->
     <!-- TODO Style cleanup Including highlighting header line.-->
     <% var_hash = @variable.values_hash -%>
	<% value_domain_hash = Hash.new %>
	<% var_hash.each_key do |key| %>
		<% @variable.value_domains.each do |value_domain| %>
			<% if value_domain.value.to_i.eql?(key.to_i) %> 
				<% value_domain_hash[key] = value_domain.label %>
				<% break %>
			<% end %>
		<% end %>
	<% end %>
	<% no_var_hash.each_key do |key| %>
		<% @variable.value_domains.each do |value_domain| %>
			<% if value_domain.value.to_i.eql?(key.to_i) %> 
				<% value_domain_hash[key] = value_domain.label %>
				<% break %>
			<% end %>
		<% end %>
	<% end %>
      <% if var_hash.size > 0 && var_hash.size <= 20 -%>
<div class="left box_standout">
        <p>
          <b>Value frequency counts:</b>
            <table class="values-table">
				<thead>
              <tr>
                <th>Value</th>
                <th>Frequency</th>
				<th>Label</th>
              <tr> 
	 			</thead>
				<tbody>
              <% var_hash.sort.each do |value, frequency| -%>
                <tr>
                  <td><%= value %></td>
                  <td><%= frequency %></td>
				  <td><%= value_domain_hash[value] ? value_domain_hash[value] : "N/A" %>
                <tr>                  
              <% end -%>
              <% no_var_hash.sort.each do |value, frequency| -%>
                <tr>
                  <td> <%= value %> </td>
                  <td> <%= frequency %> </td>
				<td><%= value_domain_hash[value] ? value_domain_hash[value] : "N/A" %>
                <tr>                  
              <% end -%>
			</tbody>
            </table>
        </p>
</div> 
		<div class="gviz-chart" id="frequency_count_chart_div"></div>
		<div style="clear:both;">
			</div>
		<% else %>
		<b>There are <%= var_hash.size -%> unique values</b>
      <% end -%> 

     <!-- TODO Style cleanup Including highlighting header line.-->
      <% unless @variable.updated_by.blank? %>
        <p><b>Metadata last updated by:
          <div class="box_standout">
            <%= link_to User.find(@variable.updated_by).person.name, person_path(User.find(@variable.updated_by).person) -%>
          </div>
        </p>
        <p><b>Reason:
          <% unless @variable.update_reason.blank? -%>
            <div class="box_standout">
              <%= @variable.update_reason -%>
            </div>
          <% else -%>
            <p class="none_text">
              No reason given
            </p>
          <% end %>
        </p>
      <% end -%>

    </div>

  <!-- </div> -->
</div>

<% if var_hash.size > 0 && var_hash.size <= 20 -%>
<script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Value');
        data.addColumn('number', 'Count');
              <% var_hash.sort.each do |value, frequency| -%>
data.addRow(['<%= value_domain_hash[value] ? escape_javascript(value_domain_hash[value].to_s) : escape_javascript(value.to_s) %>', <%= frequency %>]);
              <% end -%>
			              <% no_var_hash.sort.each do |value, frequency| -%>
			data.addRow(['<%= value_domain_hash[value] ? escape_javascript(value_domain_hash[value].to_s) : escape_javascript(value.to_s) %>', <%= frequency %>]);
			              <% end -%>

        var chart = new google.visualization.PieChart(document.getElementById('frequency_count_chart_div'));
        chart.draw(data, {width: 600, height: 400, is3D: true});
      }
    </script>
<% end %>